<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <div class="grid-content">组织机构</div>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <div class="grid-content">
          <el-dropdown size="medium" type="primary">
            <span class="el-dropdown-link">
              操作
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>

            <template #dropdown>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  icon="el-icon-circle-plus"
                  command="add"
                  @click.native="showEdit('add')"
                >
                  新增
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-edit"
                  command="edit"
                  @click.native="showEdit('edit')"
                >
                  修改
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-tree
        :data="data"
        :props="defaultProps"
        :highlight-current="true"
        :default-expand-all="true"
        @node-click="handleNodeClick"
      ></el-tree>
    </el-row>
    <el-dialog :visible.sync="dialogFormVisible1" title="组织编辑" width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-input v-model="form.id" type="hidden"></el-input>
        <el-input v-model="form.parentid" type="hidden"></el-input>

        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptname">
          <el-input v-model="form.deptname"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="enable">
          <el-radio-group v-model="form.enable">
            <el-radio label="1" model-value="enable">启用</el-radio>
            <el-radio label="0" model-value="enable">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getDeptlistTree, saveDept } from '@/api/SysDept'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        currtreenode: '',
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
          id: '0',
        },
        form: {
          id: '',
          code: '',
          birthday: '',
          enable: '1',
        },
        rules: {
          deptname: [
            { required: 1, message: '请输入部门名称', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          code: [
            { required: 1, message: '请输入部门编码', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible1: false,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleNodeClick(data) {
        //console.log(data)
        this.currtreenode = data
        //this.currtreenode = data
        //treenode = data;
        //	this.$emit('currtreenode',data);
        //console.log(this.treenode);
      },
      showEdit(flag) {
        this.title = '编辑'
        if (flag == 'edit') {
          if (this.currtreenode == '') {
            this.$baseMessage('请选机构节点', 'error')
            return false
          }
          this.form = Object.assign(
            {},
            {
              id: this.currtreenode.id,
              code: this.currtreenode.nodecode,
              deptname: this.currtreenode.label,
              parentid: this.currtreenode.parentid,
              enable: this.currtreenode.enable,
            }
          )
        } else {
          this.form = Object.assign(
            {},
            {
              id: '',
              code: '',
              deptname: '',
              parentid: this.currtreenode == '' ? '' : this.currtreenode.id,
              enable: '1',
            }
          )
        }
        this.dialogFormVisible1 = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible1 = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveDept(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
            this.fetchData()
          } else {
            return false
          }
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getDeptlistTree(this.queryForm)
        this.data = data
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
